/**
 * 
 */
package org.gaming.common.asserts;

/**
 * @author YY
 *
 */
public class BusinessException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static int stackTraceDepth = 10;

	private final ITipCause tipCause;
	private final Object[] params;
	private final String message;
	
	public BusinessException(ITipCause tipCause, Object... params) {
		this.tipCause = tipCause;
		this.params = params;
		
		StackTraceElement[] stackTrace = this.getStackTrace();
		StackTraceElement[] mineStackTrace = new StackTraceElement[Math.min(stackTraceDepth, stackTrace.length)];
		System.arraycopy(stackTrace, 0, mineStackTrace, 0, mineStackTrace.length);
		this.setStackTrace(mineStackTrace);
		
		if(params == null) {
			this.message = tipCause.getMessage();
		} else {
			String temp = tipCause.getMessage();
			for(Object param : params) {
				temp = temp.replaceFirst("%s", param.toString());
			}
			this.message = temp;
		}
	}

	public ITipCause getTipCause() {
		return tipCause;
	}

	public Object[] getParams() {
		return params;
	}
	
	@Override
	public String getMessage() {
		return message;
	}
	
	public int getCode() {
		return tipCause.getCode();
	}
}
